<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-16
 ***********************************************************************/
if(defined("IVANENGINE_CORE_DATABASE_MYSQL"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_DATABASE_MYSQL", true);

require_once (dirname(__FILE__).'/Database.php');

class CMysql extends CDatabase 
{
	public function __construct($dbhost, $dbuser, $dbpw, 
		$dbname = '', $charset = 'utf8', $pconnect = 0) 
	{
		parent::__construct($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect);
	}
	
	public function __destruct() 
	{
		parent::__destruct();
	}
	
	public function Version() 
	{
		return $this->m_version;
	}
	
	public function SelectDatabase($dbName) 
	{
		return mysql_select_db($dbName, $this->m_linkId);
	}
	
	public function SetCharset($charset) 
	{
	 	if ($this->m_version > '4.1')
        {
            if (in_array(strtolower($charset), array('gbk', 'big5', 'utf-8', 'utf8')))
            {
                $charset = str_replace('-', '', $charset);
            }
            if ($charset != 'latin1')
            {
                mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->m_linkId);
            }
        }
	}
	
	public function LastErrorMsg() 
	{
		return mysql_error($this->m_linkId);
	}

	public function LastErrorCode() 
	{
		return mysql_errno($this->m_linkId);
	}	

	public function LastInsertId() 
	{
		return mysql_insert_id($this->m_linkId);
	}
	
	public function LastAffectedRows() 
	{
		return mysql_affected_rows($this->m_linkId);
	}
	
	public function Connect() 
	{
		$dbhost		= $this->m_settings['dbhost'];
		$dbuser		= $this->m_settings['dbuser'];
		$dbpw		= $this->m_settings['dbpw'];
		$dbname		= $this->m_settings['dbname'];
		$charset	= $this->m_settings['charset'];
		$pconnect	= $this->m_settings['pconnect'];
		
	 	if ($pconnect)
        {
            if (!($this->m_linkId = @mysql_pconnect($dbhost, $dbuser, $dbpw)) )
            	throw new CException("Can't pConnect MySQL Server($dbhost)!");
        }
        else
        {
            if (PHP_VERSION >= '4.2')
            {
                $this->m_linkId = @mysql_connect($dbhost, $dbuser, $dbpw, true);
            }
            else
            {
                $this->m_linkId = @mysql_connect($dbhost, $dbuser, $dbpw);
            }
            if (!$this->m_linkId)
                throw new CException("Can't Connect MySQL Server($dbhost)!");
        }

        $this->m_version = mysql_get_server_info($this->m_linkId);

        if ($this->m_version > '4.1')
        {
            if ($charset != 'latin1')
            {
                mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->m_linkId);
            }
            if ($this->m_version > '5.0.1')
            {
                mysql_query("SET sql_mode=''", $this->m_linkId);
            }
        }
        
        if ($dbname)
        {
            if (mysql_select_db($dbname, $this->m_linkId) === false )
            {
               	CSingleton::Get("CLog")->Error("Can't select MySQL database($dbname)!");

                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return true;
        }
	}
	
	public function Disconnect() 
	{
		return mysql_close($this->m_linkId);
	}
	

	public function Query($sql) 
	{
		if ($this->m_linkId === null)
            $this->Connect();;

        if ($this->m_queryCount++ <= 99)
        {
            $this->m_queryLog->PushBack($sql);
        }

        if (!($query = mysql_query($sql, $this->m_linkId)) )
        {
            $errorMsg	= 'MySQL Query Error';
            $errorMsg	.= mysql_errno($this->m_linkId)."<br>";
            $errorMsg	.= $sql."<br>";
            $errorMsg	.= mysql_error($this->m_linkId)."<br>";

			CSingleton::Get("CLog")->Error($errorMsg);
            return false;
        }
        
        $array	= array();
		$rows	= mysql_num_rows($query);
        for ($i = 0; $i < $rows; $i++)
        {
        	$rt			= mysql_fetch_assoc($query);
        	$array[$i]	= $rt;
        }
        $result	= new CDBQueryResult($array);
        return $result;
	}
}

?>